using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Budgetor.Properties;

namespace Budgetor.Reports
{
    class RequiredReciepts : IReport
    {
        RecieptItemCollection recieptItemCollection = new RecieptItemCollection();

        #region IReport Members

        public void Generate(Data[] dataList)
        {
            foreach (Data dataItem in dataList)
            {
                if (dataItem.AmountReimbursed != 0 && dataItem.Category != "Advance")
                {
                    RecieptItem item = new RecieptItem(dataItem.Category, dataItem.Date, dataItem.Description, dataItem.AmountReimbursed);
                    recieptItemCollection.Add(item);
                }
            }

            recieptItemCollection.Sort();

            if (recieptItemCollection.Count != 0)
                Save();
        }

        private void Save()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(saveFileDialog.FileName);

                string category = string.Empty;
                int count = -1;
                float amount = 0;

                foreach (RecieptItem item in recieptItemCollection)
                {
                    if (category != item.Category)
                    {
                        if (count != -1)
                        {
                            writer.WriteLine("--------------------------------------------------");
                            writer.WriteLine(string.Format("Total Reciepts: {0}", count));
                            writer.WriteLine("Total Amount: " + amount.ToString(Resources.DollarFormat));
                        }

                        writer.WriteLine();
                        category = item.Category;
                        writer.WriteLine(category);
                        writer.WriteLine("--------------------------------------------------");
                        count = 0;
                        amount = 0;
                    }

                    string text = string.Format("{0:dd MMM yy} {1} {2}", item.Date, item.Description, item.Amount.ToString(Resources.DollarFormat));
                    writer.WriteLine(text);
                    count++;
                    amount += item.Amount;
                }

                writer.WriteLine("--------------------------------------------------");
                writer.WriteLine(string.Format("Total Reciepts: {0}", count));
                writer.WriteLine("Total Amount: " + amount.ToString(Resources.DollarFormat));
                writer.Close();
            }
        }

        #endregion
    }

    class RecieptItemCollection : List<RecieptItem>
    {

    }

    class RecieptItem: IComparable
    {
        private string category;

        public string Category
        {
            get { return category; }
            set { category = value; }
        }

        private DateTime date;

        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }
        private string description;

        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        private float amount;

        public float Amount
        {
            get { return amount; }
            set { amount = value; }
        }

        public RecieptItem(string category, DateTime date, string description, float amount)
        {
            this.category = category;
            this.date = date;
            this.description = description;
            this.amount = amount;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            RecieptItem other = obj as RecieptItem;

            int firstResult = this.category.CompareTo(other.category);

            return firstResult == 0 ? this.date.CompareTo(other.date) : firstResult;
        }

        #endregion
    }
}
